﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace EntityDA
{
    public class HoaDonDB
    {
        //lấy ra danh sách hóa đơn theo tình trạng xử lí
        public List<HoaDonData> GetAllHoaDon(int TinhTrang)
        {
            List<HoaDonData> DSHoaDon = new List<HoaDonData>();
            ConnectionDB cn = new ConnectionDB();
            string sql = "select * from HoaDon where TinhTrang=@TinhTrang";
            cn.cmd.Parameters.Add(new SqlParameter("@TinhTrang", TinhTrang));
            cn.cmd.CommandText = sql;
            SqlDataReader reader = cn.cmd.ExecuteReader();
            HoaDonData HoaDon = null;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    HoaDon = new HoaDonData();
                    if (!reader.IsDBNull(0))
                        HoaDon.MaHD = reader.GetInt32(0);
                    if (!reader.IsDBNull(1))
                        HoaDon.MaTV = reader.GetInt32(1);
                    if (!reader.IsDBNull(2))
                        HoaDon.NgayDatHang = reader.GetDateTime(2);
                    if (!reader.IsDBNull(3))
                        HoaDon.TriGia = reader.GetInt32(3);
                    if (!reader.IsDBNull(4))
                        HoaDon.DaGiaoHang = reader.GetBoolean(4);
                    if (!reader.IsDBNull(5))
                        HoaDon.NgayGiaoHang = reader.GetDateTime(5);
                    if (!reader.IsDBNull(6))
                        HoaDon.DiaChiGH = reader.GetString(6);
                    if (!reader.IsDBNull(7))
                        HoaDon.PhiGH = reader.GetDecimal(7);
                    if (!reader.IsDBNull(8))
                        HoaDon.TinhTrang = reader.GetInt32(8);
                    if (!reader.IsDBNull(9))
                        HoaDon.MaNV = reader.GetInt32(9);
                    DSHoaDon.Add(HoaDon);
                }
            }
            return DSHoaDon;
        }
        //cập nhật hóa đơn 3 tham số 
        public bool Update(int MaNV, int MaHD, int TinhTrang)
        {
            try
            {
                ConnectionDB cn = new ConnectionDB();
                string sql = "update HoaDon set MaNV=@MaNV, TinhTrang=@TinhTrang where MaHD=@MaHD";
                cn.cmd.Parameters.Add(new SqlParameter("@MaNV", MaNV));
                cn.cmd.Parameters.Add(new SqlParameter("@MaHD", MaHD));
                cn.cmd.Parameters.Add(new SqlParameter("@TinhTrang", TinhTrang));
                cn.cmd.CommandText = sql;
                cn.cmd.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool Update(int MaHD, int TinhTrang)
        {
            try
            {
                ConnectionDB cn = new ConnectionDB();
                string sql = "update HoaDon set TinhTrang=@TinhTrang where MaHD=@MaHD";
                cn.cmd.Parameters.Add(new SqlParameter("@MaHD", MaHD));
                cn.cmd.Parameters.Add(new SqlParameter("@TinhTrang", TinhTrang));
                cn.cmd.CommandText = sql;
                cn.cmd.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            return true;
        }

        //lấy ra 1 hóa đơn theo mã hóa đơn
        public HoaDonData GetOneHoaDon(int MaHD)
        {
            ConnectionDB cn = new ConnectionDB();
            string sql = "";
            if (MaHD == -1)
                return null;
            else
            {
                sql = "select * from HoaDon where MaHD=@MaHD";
                cn.cmd.Parameters.Add(new SqlParameter("@MaHD", MaHD));
            }
            cn.cmd.CommandText = sql;
            SqlDataReader reader = cn.cmd.ExecuteReader();
            HoaDonData HoaDon = null;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    HoaDon = new HoaDonData();
                    if (!reader.IsDBNull(0))
                        HoaDon.MaHD = reader.GetInt32(0);
                    if (!reader.IsDBNull(2))
                        HoaDon.NgayDatHang = reader.GetDateTime(2);
                    if (!reader.IsDBNull(3))
                        HoaDon.TriGia = reader.GetInt32(3);
                    if (!reader.IsDBNull(4))
                        HoaDon.DaGiaoHang = reader.GetBoolean(4);
                    if (!reader.IsDBNull(5))
                        HoaDon.NgayGiaoHang = reader.GetDateTime(5);
                    if (!reader.IsDBNull(6))
                        HoaDon.DiaChiGH = reader.GetString(6);
                    if (!reader.IsDBNull(7))
                        HoaDon.PhiGH = reader.GetDecimal(7);
                    if (!reader.IsDBNull(8))
                        HoaDon.TinhTrang = reader.GetInt32(8);
                    if (!reader.IsDBNull(9))
                        HoaDon.MaNV = reader.GetInt32(9);
                }
            }
            return HoaDon;
        }

        //thêm 1 hóa đơn
        public bool ThemHoaDon(HoaDonData HoaDon)
        {
            try
            {
                ConnectionDB cn = new ConnectionDB();
                string sql = "insert into HoaDon(MaTV,NgayDatHang,DiaChiGH,TinhTrang)";
                sql += "values(@MaTV,getdate(),@DiaChi,1)";
                cn.cmd.CommandText = sql;
                cn.cmd.Parameters.Add(new SqlParameter("@MaTV", HoaDon.MaTV));
                cn.cmd.Parameters.Add(new SqlParameter("@DiaChi", HoaDon.DiaChiGH));
                cn.cmd.CommandText = sql;
                cn.cmd.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}